package cre.sample.test.io.pipe;

import java.io.IOException;
import java.io.PipedInputStream;

/**
 * Created by CreGu on 2016/6/13.
 */
public class Receiver extends Thread {

    private final PipedInputStream inputStream = new PipedInputStream();

    @Override
    public void run() {
        System.out.println("receiving.......");
//        readShortMessage();
        readLongMessage();
    }

    private void readShortMessage() {
        byte[] buffer = new byte[1024];
        try {
            inputStream.read(buffer);
            System.out.println(new String(buffer));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void readLongMessage() {
        byte[] buffer = new byte[2048];
        try {
            while (true) {
                int len = inputStream.read(buffer);
                if (len == -1) {
                    break;
                }
                System.out.println(new String(buffer));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public PipedInputStream getInputStream() {
        return inputStream;
    }
}
